package com.hc.serviceimpl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hc.entity.Environmentmv;
import com.hc.entity.Incubatormv;
import com.hc.entity.QcEquipment;
import com.hc.entity.Refrigeratormv;
import com.hc.mapper.QcEquipmentMapper;
import com.hc.service.IEnvironmentmvService;
import com.hc.service.IIncubatormvService;
import com.hc.service.IRefrigeratormvService;
import com.hc.service.TriplicitiesService;
import com.hc.util.DateUtil;
import com.hc.util.RegularUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TriplicitiesServiceImpl implements TriplicitiesService {

    @Value("${tripartite.url}")
    private String tripartiteUrl;

    @Autowired
    private QcEquipmentMapper qcEquipmentMapper;

    @Autowired
    private IIncubatormvService incubatormvService;

    @Autowired
    private IRefrigeratormvService refrigeratormvService;

    @Autowired
    private IEnvironmentmvService environmentmvService;



    //以下就是外部接口需要提供的标准格式数据
    @Override
    public List<Incubatormv> getIncubatorByDate(String date, String equipmenttypeId)                                                  {
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("hospitalCode", "H0006");
        queryParams.put("date", date);
        queryParams.put("equipmenttypeId", equipmenttypeId);
        String body = HttpRequest.get(tripartiteUrl).form(queryParams).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        List<Incubatormv> incubatormvs = JSON.parseArray(jsonObject.getString("result"), Incubatormv.class);
        if (CollectionUtils.isEmpty(incubatormvs)) {
            return null;
        }
        String classifyId = null;
        if (StringUtils.equals(equipmenttypeId, "4")) {
            classifyId = "1";
        } else if (StringUtils.equals(equipmenttypeId, "2")) {
            classifyId = "4";
        } else if (StringUtils.equals(equipmenttypeId, "1")) {
            classifyId = "7";
        }
        List<QcEquipment> qcEquipments = qcEquipmentMapper.selectList(Wrappers.lambdaQuery(new QcEquipment())
                .eq(QcEquipment::getClassifyId, classifyId));
        Map<String, QcEquipment> mt100Map = new HashMap<>();
        Map<String, QcEquipment> mt300Map = new HashMap<>();
        Map<String, QcEquipment> mt700Map = new HashMap<>();
        qcEquipments.forEach(qcEquipment -> {
            String mt100Sn = qcEquipment.getMt100Sn();
            String mt300Sn = qcEquipment.getMt300Sn();
            String mt700Sn = qcEquipment.getMt700Sn();
            if (StringUtils.isNotEmpty(mt100Sn)) {
                mt100Map.put(mt100Sn, qcEquipment);
            }
            if (StringUtils.isNotEmpty(mt300Sn)) {
                mt300Map.put(mt300Sn, qcEquipment);
            }
            if (StringUtils.isNotEmpty(mt700Sn)) {
                mt700Map.put(mt700Sn, qcEquipment);
            }
        });
        incubatormvs.forEach(incubatormv -> {
            String equipmentNo = incubatormv.getEquipmentNo();
            QcEquipment qcEquipment = mt100Map.get(equipmentNo);
            if (null != qcEquipment) {
                incubatormv.setGroup(String.valueOf(qcEquipment.getId()));
                return;
            }
            QcEquipment qcEquipmentMt300 = mt300Map.get(equipmentNo);
            if (null != qcEquipmentMt300) {
                incubatormv.setGroup(String.valueOf(qcEquipmentMt300.getId()));
                return;
            }
            QcEquipment qcEquipmentMt700 = mt700Map.get(equipmentNo);
            if (null != qcEquipmentMt700) {
                incubatormv.setGroup(String.valueOf(qcEquipmentMt700.getId()));
            }
        });
        return incubatormvs;
    }

    @Override
    public void insetIIncubatormvLabSystem(String time) {
        String beforeZero;
        if (StringUtils.isEmpty(time)) {
            beforeZero = DateUtil.getBeforeZero();
        } else {
            beforeZero = time;
        }
        List<Incubatormv> incubators = getIncubatorByDate(beforeZero, "2");
        if (CollectionUtils.isEmpty(incubators)) {
            log.info("当前日期无监控数据:{}",beforeZero);
            return;
        }
        for (int i = 1; i <= 4; i++) {
            String addAfterTime1 = DateUtil.getAddAfterTime(beforeZero, (i - 1) * 6);
            String addAfterTime = DateUtil.getAddAfterTime(beforeZero, i * 6);
            if (i == 4) {
                addAfterTime = DateUtil.getAddAfterTime(beforeZero, i * 5);
            }
            String finalAddAfterTime = addAfterTime;
            List<Incubatormv> incubatormvList = incubators.stream().filter(res -> DateUtil.whetherItIsIn(DateUtil.parseDate(res.getInputTime()), DateUtil.parseDate(addAfterTime1), DateUtil.parseDate(finalAddAfterTime))).collect(Collectors.toList());
            Map<String, List<Incubatormv>> collect = incubatormvList.stream().collect(Collectors.groupingBy(Incubatormv::getEquipmentNo));
            Set<String> strings = collect.keySet();
            List<Incubatormv> list = new ArrayList<>();
            int finalI = i;
            strings.forEach(s -> {
                List<Incubatormv> incubatormvs = collect.get(s);
                if (CollectionUtils.isNotEmpty(incubatormvs)) {
                    Incubatormv incubatormv = incubatormvs.get(0);
                    Incubatormv incubatormv1 = new Incubatormv();
                    incubatormv1.setEquipmentTypeId("2");
                    incubatormv1.setEquipmentNo(incubatormv.getEquipmentNo());
                    incubatormv1.setEquipmentName(incubatormv.getGroup());
                    String equipmentTypeId = incubatormv.getInstrumentTypeId();
                    incubatormv1.setInstrumentTypeId(equipmentTypeId);
                    incubatormv1.setInputTime(addAfterTime1);
                    //MT100
                    if (StringUtils.equalsAny(equipmentTypeId, "1", "2","113")) {
                        incubatormv1.setThirdTimeInterval(String.valueOf(finalI));
                        List<Incubatormv> collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect1)) {
                            collect1.forEach(m -> {
                                String currenttemperature = m.getCurrenttemperature();
                                m.setThirdTemperatureMv(Double.valueOf(currenttemperature));
                            });
                            double sum = collect1.stream().mapToDouble(Incubatormv::getThirdTemperatureMv).sum();
                            if (sum != 0) {
                                double divide = DateUtil.divide(sum, (double) collect1.size(), 2);
                                incubatormv1.setThirdTemperatureMv(divide);
                            } else {
                                incubatormv1.setThirdTemperatureMv(0.0);
                            }
                        }
                    }
                    //MT300
                    else if (StringUtils.equalsAny(equipmentTypeId, "4", "5", "6", "7", "109","102","108","109","110","112")) {
                        incubatormv1.setThirdTimeInterval(String.valueOf(finalI));
                        //第三方温度
                        List<Incubatormv> collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect1)) {
                            collect1.forEach(m -> {
                                String currenttemperature = m.getCurrenttemperature();
                                m.setThirdTemperatureMv(Double.valueOf(currenttemperature));
                            });
                            double sum = collect1.stream().mapToDouble(Incubatormv::getThirdTemperatureMv).sum();
                            if (sum != 0) {
                                double divide = DateUtil.divide(sum, (double) collect1.size(), 2);
                                incubatormv1.setThirdTemperatureMv(divide);
                            } else {
                                incubatormv1.setThirdTemperatureMv(0.0);
                            }
                        }
                        //第三方二氧化碳浓度平均值
                        List<Incubatormv> collect2 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentcarbondioxide()) && RegularUtil.checkContainsNumbers(m.getCurrentcarbondioxide())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect2)) {
                            collect2.forEach(n -> {
                                String currentcarbondioxide = n.getCurrentcarbondioxide();
                                n.setThirdCdMv(Double.valueOf(currentcarbondioxide));
                            });
                            double sum1 = collect2.stream().mapToDouble(Incubatormv::getThirdCdMv).sum();
                            if (sum1 != 0) {
                                double divide = DateUtil.divide(sum1, (double) collect2.size(), 2);
                                incubatormv1.setThirdCdMv(divide);
                            } else {
                                incubatormv1.setThirdCdMv(0.0);
                            }
                        }
                        //第三方氧气浓度平均值
                        List<Incubatormv> collect3 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrento2()) && RegularUtil.checkContainsNumbers(m.getCurrento2())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect3)) {
                            collect3.forEach(n -> {
                                String currento2 = n.getCurrento2();
                                n.setThirdYqMv(Double.valueOf(currento2));
                            });
                            double sum2 = collect3.stream().mapToDouble(Incubatormv::getThirdYqMv).sum();
                            if (sum2 != 0) {
                                double divide = DateUtil.divide(sum2, (double) collect3.size(), 2);
                                incubatormv1.setThirdYqMv(divide);
                            } else {
                                incubatormv1.setThirdYqMv(0.0);
                            }
                        }
                    }//MT700 G185单独判断
                    else {
                        incubatormv1.setTimeInterval(String.valueOf(finalI));
                        List<Incubatormv> collect1;
                        //温度平均值
                        if (StringUtils.equalsAnyIgnoreCase(equipmentTypeId, "12","14","105","13","100","97","100","101","105","111","115","117")) {
                            collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature1()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature1())).collect(Collectors.toList());
                            if (CollectionUtils.isNotEmpty(collect1)) {
                                collect1.forEach(m -> {
                                    String currenttemperature1 = m.getCurrenttemperature1();
                                    m.setMeanValue(Double.valueOf(currenttemperature1));
                                });
                            }
                        } else {
                            collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature())).collect(Collectors.toList());
                            if (CollectionUtils.isNotEmpty(collect1)) {
                                collect1.forEach(m -> {
                                    String currenttemperature = m.getCurrenttemperature();
                                    m.setMeanValue(Double.valueOf(currenttemperature));
                                });
                            }
                        }
                        if (CollectionUtils.isNotEmpty(collect1)) {
                            double sum = collect1.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                            if (sum != 0) {
                                double divide = DateUtil.divide(sum, (double) collect1.size(), 2);
                                incubatormv1.setMeanValue(divide);
                            } else {
                                incubatormv1.setMeanValue(0.0);
                            }
                        }
                        //二氧化碳
                        List<Incubatormv> collect2 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentcarbondioxide()) && RegularUtil.checkContainsNumbers(m.getCurrentcarbondioxide())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect2)) {
                            collect2.forEach(n -> {
                                String currentcarbondioxide = n.getCurrentcarbondioxide();
                                n.setCdMv(Double.valueOf(currentcarbondioxide));
                            });
                            double sum1 = collect2.stream().mapToDouble(Incubatormv::getCdMv).sum();
                            if (sum1 != 0) {
                                double divide = DateUtil.divide(sum1, (double) collect2.size(), 2);
                                incubatormv1.setCdMv(divide);
                            } else {
                                incubatormv1.setCdMv(0.0);
                            }
                        }
                        //氧气平均值
                        List<Incubatormv> collect3 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrento2()) && RegularUtil.checkContainsNumbers(m.getCurrento2())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect3)) {
                            collect3.forEach(n -> {
                                String currento2 = n.getCurrento2();
                                n.setOxygenMv(Double.valueOf(currento2));
                            });
                            double sum2 = collect3.stream().mapToDouble(Incubatormv::getOxygenMv).sum();
                            if (sum2 != 0) {
                                double divide = DateUtil.divide(sum2, (double) collect3.size(), 2);
                                incubatormv1.setOxygenMv(divide);
                            } else {
                                incubatormv1.setOxygenMv(0.0);
                            }
                        }
                        //湿度平均值
                        List<Incubatormv> collect4 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenthumidity()) && RegularUtil.checkContainsNumbers(m.getCurrenthumidity())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect4)) {
                            collect4.forEach(n -> {
                                String currenthumidity = n.getCurrenthumidity();
                                n.setHumidityMv(Double.valueOf(currenthumidity));
                            });
                            double sum3 = collect4.stream().mapToDouble(Incubatormv::getHumidityMv).sum();
                            if (sum3 != 0) {
                                double divide = DateUtil.divide(sum3, (double) collect4.size(), 2);
                                incubatormv1.setHumidityMv(divide);
                            } else {
                                incubatormv1.setHumidityMv(0.0);
                            }
                        }
                    }
                    list.add(incubatormv1);
                }
            });
            if (CollectionUtils.isNotEmpty(list)) {
                incubatormvService.saveBatch(list);
            }
        }
    }

    @Override
    public void insetRefrigeratormv(String time) {
        String beforeZero;
        //如果时间为空
        if (StringUtils.isEmpty(time)) {
            beforeZero = DateUtil.getBeforeZero();
        } else {
            beforeZero = time;
        }
        List<Incubatormv> refrigeratorMvs = getIncubatorByDate(beforeZero, "4");
        if (CollectionUtils.isEmpty(refrigeratorMvs)) {
            log.info("当前日期无监控数据:{}",beforeZero);
            return;
        }
        List<Refrigeratormv> refrigeratormvs = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            String addAfterTime1 = DateUtil.getAddAfterTime(beforeZero, (i - 1) * 3);
            String addAfterTime = DateUtil.getAddAfterTime(beforeZero, i * 3);
            if (i == 8) {
                addAfterTime = DateUtil.getEndOfDay(beforeZero);
            }
            String finalAddAfterTime = addAfterTime;
            List<Incubatormv> incubators = refrigeratorMvs.stream().filter(res -> DateUtil.whetherItIsIn(DateUtil.parseDate(res.getInputTime()), DateUtil.parseDate(addAfterTime1), DateUtil.parseDate(finalAddAfterTime))).collect(Collectors.toList());
            Map<String, List<Incubatormv>> collect = incubators.stream().collect(Collectors.groupingBy(Incubatormv::getEquipmentNo));
            Set<String> strings = collect.keySet();
            for (String s : strings) {
                List<Incubatormv> incubatormvs = collect.get(s);
                Incubatormv incubatormv = incubatormvs.get(0);
                Refrigeratormv refrigeratormv = new Refrigeratormv();
                refrigeratormv.setEquipmentNo(s);
                //设置开始时间段属性
                refrigeratormv.setInputTime(addAfterTime1);
                //设置设备id
                refrigeratormv.setEquipmentTypeId("4");
                //获得设备名称,及质控系统设备主键id
                refrigeratormv.setEquipmentName(incubatormv.getGroup());
                //将i转化为strng类型,set到refrigeratorm里面
                refrigeratormv.setTimeInterval(String.valueOf(i));
                if (CollectionUtils.isNotEmpty(incubatormvs)) {
                    List<Incubatormv> collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(collect1)) {
                        collect1.forEach(p -> p.setMeanValue(Double.valueOf(p.getCurrenttemperature())));
                        double sum = collect1.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                        if (sum != 0) {
                            double divide = DateUtil.divide(sum, collect1.size(), 2);
                            refrigeratormv.setMeanValue(divide);
                        } else {
                            refrigeratormv.setMeanValue(null);
                        }
                    } else {
                        refrigeratormv.setMeanValue(null);
                    }
                }
                refrigeratormvs.add(refrigeratormv);
            }
        }
        if (CollectionUtils.isNotEmpty(refrigeratormvs)) {
            List<Refrigeratormv> collect = refrigeratormvs.stream().filter(refrigeratormv -> StringUtils.isNotEmpty(refrigeratormv.getEquipmentName())).collect(Collectors.toList());
            refrigeratormvService.saveBatch(collect);
        }
    }

    @Override
    public void insetEnvironmentmvmv(String time) {
        String beforeZero;
        if (StringUtils.isEmpty(time)) {
            beforeZero = DateUtil.getBeforeZero();
        } else {
            beforeZero = time;
        }
        List<Incubatormv> incubators = getIncubatorByDate(beforeZero, "1");
        if (CollectionUtils.isEmpty(incubators)) {
            log.info("当前日期无监控数据:{}",beforeZero);
            return;
        }
        List<Environmentmv> list = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            String addAfterTime1 = DateUtil.getAddAfterTime(beforeZero, (i - 1) * 6);
            String addAfterTime = DateUtil.getAddAfterTime(beforeZero, i * 6);
            if (i == 4) {
                addAfterTime = DateUtil.getAddAfterTime(beforeZero, i * 5);
            }
            String finalAddAfterTime = addAfterTime;
            List<Incubatormv> incubatormvList = incubators.stream().filter(res -> DateUtil.whetherItIsIn(DateUtil.parseDate(res.getInputTime()), DateUtil.parseDate(addAfterTime1), DateUtil.parseDate(finalAddAfterTime))).collect(Collectors.toList());
            Map<String, List<Incubatormv>> collect = incubatormvList.stream().collect(Collectors.groupingBy(Incubatormv::getEquipmentNo));
            Set<String> strings = collect.keySet();
            int count = i;
            strings.forEach(s -> {
                List<Incubatormv> incubatormvs = collect.get(s);
                Incubatormv environmentmv1 = incubatormvs.get(0);
                Environmentmv environmentmv = new Environmentmv();
                environmentmv.setEquipmentName(environmentmv1.getGroup());
                environmentmv.setEquipmentNo(environmentmv1.getEquipmentNo());
                environmentmv.setInputTime(addAfterTime1);
                environmentmv.setTimeInterval(String.valueOf(count));
                environmentmv.setEquipmentTypeId("1");
                environmentmv.setInstrumentTypeId("8");
                //温度
                List<Incubatormv> collect1 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenttemperature()) && RegularUtil.checkContainsNumbers(m.getCurrenttemperature())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect1)) {
                    collect1.forEach(m -> {
                        String currenttemperature = m.getCurrenttemperature();
                        m.setMeanValue(Double.valueOf(currenttemperature));
                    });
                    double sum = collect1.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum != 0) {
                        double divide = DateUtil.divide(sum, (double) collect1.size(), 2);
                        environmentmv.setMeanValue(divide);
                    } else {
                        environmentmv.setMeanValue(0.0);
                    }
                }
                //二氧化碳
                List<Incubatormv> collect2 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentcarbondioxide()) && RegularUtil.checkContainsNumbers(m.getCurrentcarbondioxide())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect2)) {
                    collect2.forEach(n -> {
                        String currentcarbondioxide = n.getCurrentcarbondioxide();
                        n.setCdMv(Double.valueOf(currentcarbondioxide));
                    });
                    double sum1 = collect2.stream().mapToDouble(Incubatormv::getCdMv).sum();
                    if (sum1 != 0) {
                        double divide = DateUtil.divide(sum1, (double) collect2.size(), 2);
                        environmentmv.setCdMv(divide);
                    } else {
                        environmentmv.setCdMv(0.0);
                    }
                }
                //氧气
                List<Incubatormv> collect3 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrento2()) && RegularUtil.checkContainsNumbers(m.getCurrento2())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect3)) {
                    collect3.forEach(n -> {
                        String currento2 = n.getCurrento2();
                        n.setOxygenMv(Double.valueOf(currento2));
                    });
                    double sum2 = collect3.stream().mapToDouble(Incubatormv::getOxygenMv).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect3.size(), 2);
                        environmentmv.setOxygenMv(divide);
                    } else {
                        environmentmv.setOxygenMv(0.0);
                    }
                }
                //气流压力
                List<Incubatormv> collect4 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentairflow()) && RegularUtil.checkContainsNumbers(m.getCurrentairflow())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect4)) {
                    collect4.forEach(n -> {
                        String currentairflow = n.getCurrentairflow();
                        n.setMeanValue(Double.valueOf(currentairflow));
                    });
                    double sum2 = collect4.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect4.size(), 2);
                        environmentmv.setPressureMv(String.valueOf(divide));
                    } else {
                        environmentmv.setPressureMv("0.0");
                    }
                }
                //湿度
                List<Incubatormv> collect5 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrenthumidity()) && RegularUtil.checkContainsNumbers(m.getCurrenthumidity())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect5)) {
                    collect5.forEach(n -> {
                        String currenthumidity = n.getCurrenthumidity();
                        n.setMeanValue(Double.valueOf(currenthumidity));
                    });
                    double sum2 = collect5.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect5.size(), 2);
                        environmentmv.setHumidityMv(divide);
                    } else {
                        environmentmv.setHumidityMv(0.0);
                    }
                }
                //voc
                List<Incubatormv> collect6 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentvoc()) && RegularUtil.checkContainsNumbers(m.getCurrentvoc())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect6)) {
                    collect6.forEach(n -> {
                        String currentvoc = n.getCurrentvoc();
                        n.setMeanValue(Double.valueOf(currentvoc));
                    });
                    double sum2 = collect6.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect6.size(), 2);
                        environmentmv.setVocMv(String.valueOf(divide));
                    } else {
                        environmentmv.setVocMv("0.0");
                    }
                }
                //甲醛
                List<Incubatormv> collect7 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentformaldehyde()) && RegularUtil.checkContainsNumbers(m.getCurrentformaldehyde())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect7)) {
                    collect7.forEach(n -> {
                        String currentformaldehyde = n.getCurrentformaldehyde();
                        n.setMeanValue(Double.valueOf(currentformaldehyde));
                    });
                    double sum2 = collect7.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect7.size(), 2);
                        environmentmv.setFdMv(String.valueOf(divide));
                    } else {
                        environmentmv.setFdMv("0.0");
                    }
                }
                //pm2.5
                List<Incubatormv> collect8 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentpm25()) && RegularUtil.checkContainsNumbers(m.getCurrentpm25())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect8)) {
                    collect8.forEach(n -> {
                        String currentpm25 = n.getCurrentpm25();
                        n.setMeanValue(Double.valueOf(currentpm25));
                    });
                    double sum2 = collect8.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect8.size(), 2);
                        environmentmv.setPm2Mv(divide);
                    } else {
                        environmentmv.setPm2Mv(0.0);
                    }
                }
                //pm10
                List<Incubatormv> collect9 = incubatormvs.stream().filter(m -> StringUtils.isNotEmpty(m.getCurrentpm10()) && RegularUtil.checkContainsNumbers(m.getCurrentpm10())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect9)) {
                    collect9.forEach(n -> {
                        String currentpm10 = n.getCurrentpm10();
                        n.setMeanValue(Double.valueOf(currentpm10));
                    });
                    double sum2 = collect9.stream().mapToDouble(Incubatormv::getMeanValue).sum();
                    if (sum2 != 0) {
                        double divide = DateUtil.divide(sum2, (double) collect9.size(), 2);
                        environmentmv.setPm10Mv(divide);
                    } else {
                        environmentmv.setPm10Mv(0.0);
                    }
                }
                list.add(environmentmv);
            });
        }
        if (CollectionUtils.isNotEmpty(list)){
            List<Environmentmv> collect = list.stream().filter(env -> StringUtils.isNotEmpty(env.getEquipmentName())).collect(Collectors.toList());
            environmentmvService.saveBatch(collect);
        }
    }
}
